<template>
  <div>
    <el-dialog
      title="编辑数据源信息"
      :form="form"
      :editDataSourceFormVisible="editDataSourceFormVisible"
      @editDataSourceOk="editDataSourceOk"
      @editDataSourceCancel="editDataSourceFormVisible = false"
    ></el-dialog>
    <el-dialog
      title="添加数据源信息"
      :form="addDateSourceForm"
      :addDataSourceFormVisible="addDataSourceFormVisible"
      @addDataSourceOk="addDataSourceOk"
      @addDataSourceCancel="addDataSourceFormVisible = false"
    ></el-dialog>
    <el-row>
       <el-col :span="16" style="height:1px"></el-col>
      <el-col :span="4">
        <div class="addOperation" @click="addDataSource">
          <i class="el-icon-circle-plus-outline" style="margin-right:5px"></i>
          <span>添加数据源</span>
        </div>
      </el-col>
    </el-row>
    <el-table :data="tableData">
      <el-table-column prop="dataSourceId" label="dataSourceId">
      </el-table-column>
      <el-table-column prop="dataSourceType" label="dataSourceType">
      </el-table-column>
      <el-table-column prop="databaseName" label="databaseName">
      </el-table-column>
      <el-table-column prop="dataSourceLink" label="dataSourceLink">
      </el-table-column>
      <el-table-column
        prop="dataSourceUser"
        label="dataSourceUser"
      ></el-table-column>
      <el-table-column label="edit">
        <template slot-scope="scope">
          <img
            src="../../assets/images/userManage/edit.png"
            alt="edit"
            @click="editDataSource(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="delete">
        <template slot-scope="scope">
          <el-popconfirm
            title="确定删除数据源吗？"
            @confirm="deleteDataSource(scope.row.dataSourceId)"
          >
            <img
              slot="reference"
              src="../../assets/images/userManage/delete.png"
              alt="delete"
            />
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { request } from "../../utils/index.js";
import elDialog from "./dialog.vue";
export default {
  data() {
    return {
      tableData: [],
      formLabelWidth: "120px",
      form: {},
      editDataSourceFormVisible: false,

      addDateSourceForm: {},
      addDataSourceFormVisible: false,
    };
  },
  components: {
    elDialog,
  },
  watch: {},
  created() {
    this.getDateSource();
  },
  methods: {
    getDateSource() {
      request({
        method: "GET",
        url: this.portInterface.getDataSource,
      }).then((res) => {
        this.tableData = res.result;
        // console.log("zct tableData", this.tableData);
      });
    },
    deleteDataSource(id) {
      request({
        method: "PUT",
        url: this.portInterface.deleteDataSource,
        params: {
          dataSourceId: id,
        },
      }).then(() => {
        this.$message({
          showClose: true,
          message: "删除成功,正在更新页面",
          type: "success",
        });
        this.getDateSource();
      });
    },

    addDataSource() {
      this.addDataSourceFormVisible = true;
      this.addDateSourceForm = {
        dataSourceLink: "",
        dataSourcePassword: "",
        dataSourceType: "",
        dataSourceUser: "",
        databaseName: "",
      };
    },
    addDataSourceOk(userInfo) {
      // console.log("zct userInfo", userInfo);
      request({
        method: "POST",
        url: this.portInterface.addDataSource,
        data: {
          ...userInfo,
        },
      }).then(() => {
        this.$message({
          showClose: true,
          message: "添加成功,正在更新页面",
          type: "success",
        });
        this.addDataSourceFormVisible = false;
        this.getDateSource();
      });
    },
    editDataSource(datasourceInfo) {
      this.editDataSourceFormVisible = true;
      this.form = datasourceInfo;
    },
    editDataSourceOk(form) {
      request({
        method: "PUT",
        url: this.portInterface.editDateSource,
        data: {
          ...form,
        },
      }).then(() => {
        this.$message({
          showClose: true,
          message: "修改成功,正在更新页面",
          type: "success",
        });
        this.editDataSourceFormVisible = false;
        this.getDateSource();
      });
    },
  },
};
</script>
<style lang="less" scoped>
img {
  width: 25px;
}
.el-row {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  .el-col {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.userMsg {
  height: 100%;
  right: 20px;
  top: 0;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  div {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .loginOut {
    padding: 5px;
    padding-bottom: 20px;
  }
  span {
    // font-size: 0.25rem;
  }
}

@media screen and (max-width: 1200px) {
  .userMsg {
    span {
      //   font-size: 0.6rem;
    }
  }
}
</style>